import os.path
import subprocess

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QGridLayout, QLabel, QComboBox, QMessageBox

from get_users import get_users


class BlockUser(QWidget):

    def block_function(self):
        selected_user = self.users_combobox.currentData()
        if not os.path.isfile(f'/home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc'):
            subprocess.run(f'touch /home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc', shell=True)
        subprocess.run(f'chown root:{selected_user}/home/{selected_user}/.config/plasma-org.kde.plasma.desktop'
                       f'-appletsrc', shell=True)
        subprocess.run(f'chmod 640 /home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc',
                       shell=True)
        subprocess.run(f'chattr +i /home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc',
                       shell=True)

        if os.path.isfile(f'/home/{selected_user}/.config/systemsettingsrc'):
            subprocess.run(f'chown root:{selected_user} /home/{selected_user}/.config/systemsettingsrc', shell=True)
            subprocess.run(f'chmod 640/home/{selected_user}/.config/systemsettingsrc', shell=True)
            subprocess.run(f'chattr +i /home/{selected_user}/.config/systemsettingsrc', shell=True)

        if os.path.isfile(f'/home/{selected_user}/.config/kcmfonts'):
            subprocess.run(f'sed -i -r "s/^(forceFontDPI=).*/\10/" /home/{selected_user}/.config/kcmfonts', shell=True)
        else:
            subprocess.run(f'echo "[General]" > /home/{selected_user}/.config/kcmfonts', shell=True)
            subprocess.run(f'echo "forceFontDPI=0" >> /home/{selected_user}/.config/kcmfonts', shell=True)
        subprocess.run(f'chown root:{selected_user} /home/{selected_user}/.config/kcmfonts', shell=True)
        subprocess.run(f'chmod 640 /home/{selected_user}/.config/kcmfonts', shell=True)
        subprocess.run(f'chattr +i /home/{selected_user}/.config/kcmfonts', shell=True)

        if not os.path.isfile(f'/home/{selected_user}/.config/kdeglobals'):
            subprocess.run(f'touch /home/{selected_user}/.config/kdeglobals')
        subprocess.run(f'chown root:{selected_user} /home/{selected_user}/.config/kdeglobals', shell=True)
        subprocess.run(f'chmod 640 /home/{selected_user}/.config/kdeglobals', shell=True)
        subprocess.run(f'chattr +i /home/{selected_user}/.config/kdeglobals', shell=True)

        if os.path.isfile(f'/home/{selected_user}/default.png'):
            subprocess.run(f'chown root:{selected_user} /home/{selected_user}/default.png', shell=True)
            subprocess.run(f'chmod 640 /home/{selected_user}/default.png', shell=True)
            subprocess.run(f'chattr +i /home/{selected_user}/default.png', shell=True)

        if os.path.isdir(f'/home/{selected_user}/Рабочий\ стол'):
            subprocess.run(f'chown root:{selected_user} "/home/{selected_user}/Рабочий стол"', shell=True)
            subprocess.run(f'chmod 750 "/home/{selected_user}/Рабочий стол"', shell=True)
            subprocess.run(f'chattr +i "/home/{selected_user}/Рабочий стол"', shell=True)

        dlg = QMessageBox()
        dlg.setWindowTitle('Внимание!')
        dlg.setText(f'Настройки пользователя {selected_user} заблокированы.')
        dlg.exec()

    def unblock_function(self):
        selected_user = self.users_combobox.currentData()
        if os.path.isfile(f'/home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc'):
            subprocess.run(f'chattr -i /home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc',
                           shell=True)
            subprocess.run(
                f'chown {selected_user}:{selected_user} /home/{selected_user}/.config/plasma-org.kde.plasma.desktop'
                f'-appletsrc',
                shell=True)
            subprocess.run(f'chmod 660 /home/{selected_user}/.config/plasma-org.kde.plasma.desktop-appletsrc',
                           shell=True)

        if os.path.isfile(f'/home/{selected_user}/.config/systemsettingsrc'):
            subprocess.run(f'chattr -i /home/{selected_user}/.config/systemsettingsrc', shell=True)
            subprocess.run(f'chown {selected_user}:{selected_user} /home/{selected_user}/.config/systemsettingsrc',
                           shell=True)
            subprocess.run(f'chmod 660 /home/{selected_user}/.config/systemsettingsrc', shell=True)

        if os.path.isfile(f'/home/{selected_user}/.config/kcmfonts'):
            subprocess.run(f'chattr -i /home/{selected_user}/.config/kcmfonts', shell=True)
            subprocess.run(f'sed -i -r "s/^(forceFontDPI=).*/\10/" /home/{selected_user}/.config/kcmfonts', shell=True)
            subprocess.run(f'chown {selected_user}:{selected_user} /home/{selected_user}/.config/kcmfonts', shell=True)
            subprocess.run(f'chmod 660 /home/{selected_user}/.config/kcmfonts', shell=True)

        if os.path.isfile(f'/home/{selected_user}/.config/kdeglobals'):
            subprocess.run(f'chattr -i /home/{selected_user}/.config/kdeglobals', shell=True)
            subprocess.run(f'chown {selected_user}:{selected_user} /home/{selected_user}/.config/kdeglobals',
                           shell=True)
            subprocess.run(f'chmod 660 /home/{selected_user}/.config/kdeglobals', shell=True)

        if os.path.isfile(f'/home/{selected_user}/default.png'):
            subprocess.run(f'chattr -i /home/{selected_user}/default.png', shell=True)
            subprocess.run(f'chown {selected_user}:{selected_user} /home/{selected_user}/default.png', shell=True)
            subprocess.run(f'chmod 660 /home/{selected_user}/default.png', shell=True)

        if os.path.isdir(f'/home/{selected_user}/Рабочий\ стол'):
            subprocess.run(f'chattr -i "/home/{selected_user}/Рабочий стол"', shell=True)
            subprocess.run(f'chown {selected_user}:{selected_user} "/home/{selected_user}/Рабочий стол"', shell=True)
            subprocess.run(f'chmod 770 "/home/{selected_user}/Рабочий стол"', shell=True)

        dlg = QMessageBox()
        dlg.setWindowTitle('Внимание!')
        dlg.setText(f'Настройки пользователя {selected_user} разблокированы.')
        dlg.exec()

    def block_user_action(self):
        if self.block_user_actions.currentIndex() == 0:
            self.block_function()
        else:
            self.unblock_function()

    def __init__(self, block=True):
        super().__init__()
        self.block = block  # True, если нужно заблокировать, False - для разблокировки

        layout = QGridLayout()
        self.setLayout(layout)
        block_user_label = QLabel('Блокировка рабочего стола учётной записи')
        block_user_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(block_user_label, 0, 0, 1, 2)
        users = get_users()
        self.users_combobox = QComboBox()
        for user in users.keys():
            if users[user] not in (user, ''):
                self.users_combobox.addItem(f'{user} ({users[user]})', user)
            else:
                self.users_combobox.addItem(user, user)

        username_label = QLabel('Учётная запись: ')
        username_label.setAlignment(Qt.AlignRight)
        layout.addWidget(username_label, 1, 0)
        layout.addWidget(self.users_combobox, 1, 1)

        self.block_user_actions = QComboBox()
        self.block_user_actions.addItems(['Заблокировать', 'Разблокировать'])
        action_label = QLabel('Действие: ')
        action_label.setAlignment(Qt.AlignRight)
        layout.addWidget(action_label, 2, 0)
        layout.addWidget(self.block_user_actions, 2, 1)

        # ok_button = QPushButton()
        # ok_button.setText('Заблокировать' if block else 'Разблокировать')
        # if block:
        #     ok_button.clicked.connect(self.block_function)
        # else:
        #     ok_button.clicked.connect(self.unblock_function)
        # layout.addWidget(ok_button, 1, 0, 1, 2)
        # self.show()
        layout.addWidget(QLabel(''), 3, 0)
        layout.setRowStretch(0, 0)
        layout.setRowStretch(1, 0)
        layout.setRowStretch(2, 0)
        layout.setRowStretch(3, 1)
